﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using WebApplicationRating.DB.model;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplicationRating
{
    public partial class UserEntry : System.Web.UI.Page
    {

        private void InsertUserInfo(string LastName)
        {
        }

        private void DeleteUserInfo(string ID)
        {

            using (imageEntities context = new imageEntities())
            {

                //var presentUser = (from u in context.RegisteredUsers

                             //   where u.userid == ID
                               // select u).First();


                //context.RegisteredUsers.Delete(presentUser);

                //context.SubmitChanges();

                //LiteralMessage.Text = "<p style='color:Green;'>Information Deleted!</p>";

            }

        }

        // static variable

        static string prevPage = String.Empty;


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                try
                {
                    prevPage = Request.UrlReferrer.ToString();
                }
                catch
                {
                }
            }



        }

        protected void btnBack_Click(object sender, EventArgs e)
        {

            Response.Redirect(prevPage);

        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {

            lblMessageC.Text = "";
            lblMessageD.Text = "";
            lblMessageR.Text = "";
            lblMessageU.Text = "";

            try
            {
                using (imageEntities ctx = new imageEntities())
                {

                    //first check they should have used update not add
                    try
                    {

                        using (imageEntities ctxchk = new imageEntities())
                        {

                            var newInt = Convert.ToInt32(tbID.Text);
                            string newUname = tbUserName.Text;
                            string newPass = tbPassword.Text;
                            string newFirst = tbFirstName.Text;
                            string newLast = tbLastName.Text;

                            //Get the specific user from Database
                            try
                            {
                                RegisteredUser u = (from u1 in ctxchk.RegisteredUsers

                                                    where (u1.alias == newUname) && (u1.password == newPass) && (u1.firstname == newFirst) && (u1.lastname == newLast)
                                                    select u1).First();

                                lblMessageI.Text = "Existing Record Found, please use Update.";
                            }
                            catch
                            {
                                RegisteredUser u = (from u1 in ctxchk.RegisteredUsers

                                                    where (u1.alias == newUname) && (u1.password == newPass)
                                                    select u1).First();

                                lblMessageI.Text = "Existing Record Found, please use Update. RegID Found = ";
                            }

                        }
                    }
                    catch
                    {
                      //do nothing, it is an add

                    /**this works on a test table with only one field, lastname**/
                    RegisteredUser Newuser = new RegisteredUser(); //{ lastname = "NewName" };
                    Newuser.firstname = tbFirstName.Text;
                    Newuser.lastname = tbLastName.Text;
                    Newuser.email = tbEmail.Text;
                    Newuser.alias = tbUserName.Text;
                    Newuser.password = tbPassword.Text;
                    Newuser.create_date = DateTime.Now;
                    tbID.Text = "";

                    if (tbUserName.Text == null || tbUserName.Text == "" || tbPassword == null || tbPassword.Text == "")
                    {
                        lblMessageI.Text = "Name, Username and Password are Required!";
                    }
                    else
                    {
                        ctx.AddToRegisteredUsers(Newuser);
                        ctx.SaveChanges();

                        tbID.Text = Convert.ToString(Newuser.userid);

                        if (tbLastName.Text != "")
                        {

                            lblMessageI.Text = "Data Inserted!";
                        }
                        else
                        {
                            lblMessageI.Text = "Not enough information for Insert!  (Name, Username, Password required)";
                        }
                    }
                }
            }

            } //inner catch from existing record in dbase chk
            catch
            {
                lblMessageI.Text = "Insert unable to perform.";
            }

            }   //End Add Button Event

        protected void btnRtv_Click(object sender, EventArgs e)
        {
            lblMessageC.Text = "";
            lblMessageD.Text = "";
            lblMessageI.Text = "";
            lblMessageU.Text = "";

            try
            {

                using (imageEntities ctx = new imageEntities())
                {
                    string newUname = tbUserName.Text;
                    string newPass = tbPassword.Text;
                    string newFirst = tbFirstName.Text;
                    string newLast = tbLastName.Text;

                    RegisteredUser u = (from u1 in ctx.RegisteredUsers

                                        where (u1.alias == newUname) && (u1.password == newPass)
                                        select u1).First();

                    lblMessageI.Text = "UserName and Password Found.";

                    tbID.Text = Convert.ToString(u.userid);
                    tbLastName.Text = u.lastname;
                    tbFirstName.Text = u.firstname;
                    tbEmail.Text = u.email;
                    tbUserName.Text = u.alias;
                    tbPassword.Text = u.password;
                }
                }
            catch
            {

                tbLastName.Text = "";
                tbFirstName.Text = "";
                tbEmail.Text = "";
                tbUserName.Text = "";
                tbPassword.Text = "";
                lblMessageR.Text = "Could not retrieve by Username and Password provided!";
            }

        }
        //End Retrieve Button Event

        protected void btnUpd_Click(object sender, EventArgs e)
        {

            lblMessageC.Text = "";
            lblMessageD.Text = "";
            lblMessageR.Text = "";
            lblMessageI.Text = "";

            try
            {

                using (imageEntities ctx = new imageEntities())
                {

                    var newInt = Convert.ToInt32(tbID.Text);
                    
                    //Get the specific user from Database

                    RegisteredUser u = (from u1 in ctx.RegisteredUsers

                                        where u1.userid == newInt
                                        select u1).First();

                    //Change the Employee Name in memory

                    var holdID = u.userid;
                    if (tbLastName.Text != u.lastname && tbFirstName.Text != u.firstname) 
                    {
                        lblMessageU.Text = "regID mismatch " + holdID +" please retrieve your user before updating!";
                    }
                    else
                    {
                       u.firstname = tbFirstName.Text;
                       u.lastname = tbLastName.Text;
                       u.email = tbEmail.Text;
                       u.alias = tbUserName.Text;
                       u.password = tbPassword.Text;
                       u.create_date = DateTime.Now;
                    //Save to database

                    ctx.SaveChanges();

                    tbID.Text = Convert.ToString(u.userid);

                    lblMessageU.Text = "Data Updated!";
                 }                
              }
            }
            catch
            {

                lblMessageU.Text = "Update Unable to Perform. Information Invalid.";

            }

        }
//End Update Button Event

        protected void btnDel_Click(object sender, EventArgs e)
        {


            lblMessageC.Text = "";
            lblMessageI.Text = "";
            lblMessageR.Text = "";
            lblMessageU.Text = "";

            try
            {

                using (imageEntities ctx = new imageEntities())
                {

                    var newInt = Convert.ToInt32(tbID.Text);

                    //Get the specific employee from Database

                    RegisteredUser u = (from u1 in ctx.RegisteredUsers

                                        where u1.userid == newInt
                                        select u1).First();


                    //Delete it from memory

                    ctx.DeleteObject(u);

                    //Save to database

                    ctx.SaveChanges();

                    tbID.Text = "";
                    tbLastName.Text = "";
                    tbFirstName.Text = "";
                    tbEmail.Text = "";
                    tbUserName.Text = "";
                    tbPassword.Text = "";
                    lblMessageD.Text = "Data Deleted by userid!";
                }
            }
            catch
            {
                lblMessageD.Text = "Delete Unable to Perform.  Record not located.";
            }

        }
//End Delete Button Event
        protected void btnClr_Click(object sender, EventArgs e)
        {
            tbID.Text = "";
            tbLastName.Text = "";
            tbFirstName.Text = "";
            tbEmail.Text = "";
            tbUserName.Text = "";
            tbPassword.Text = "";
            lblMessageC.Text = "";
            lblMessageD.Text = "";
            lblMessageI.Text = "";
            lblMessageR.Text = "";
        }
//End Clear Button Event


}


}



